package cn.house.controller.user;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.house.common.LayuiTableResult;
import cn.house.common.Pagination;
import cn.house.common.PublicResultJosn;
import cn.house.controller.BaseController;
import cn.house.entity.CardInfo;
import cn.house.service.CardInfoService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyong
 * @since 2019-03-03
 */
@RestController
@RequestMapping("/ggfs/card-info")
public class CardInfoController extends BaseController {

	@Autowired
	private CardInfoService cardInfoService;

	/**
	 * 新增用户身份证信息
	 */
	public PublicResultJosn add(CardInfo cardInfo) {
		cardInfoService.save(cardInfo);
		return new PublicResultJosn();
	}

	/**
	 * 删除用户信息（禁用）
	 */
	public PublicResultJosn delete(@RequestBody List<Integer> ids) {
		List<CardInfo> cardInfos = ids.parallelStream().map(id -> {
			CardInfo card = new CardInfo();
			card.setId(id);
			card.setStatus(0);
			return card;
		}).collect(Collectors.toList());
		cardInfoService.updateBatchById(cardInfos);
		return new PublicResultJosn();
	}

	/**
	 * 根据id更新数据
	 */
	public PublicResultJosn updateById(CardInfo entity) {
		cardInfoService.updateById(entity);
		return new PublicResultJosn();
	}

	/**
	 * 根据id查询数据
	 */
	public PublicResultJosn selectById(Integer id) {
		CardInfo userInfo = cardInfoService.getById(id);
		return new PublicResultJosn(userInfo);
	}

	/**
	 * 分页模糊查询
	 */
	public LayuiTableResult selectByPage(Pagination pagination, CardInfo userInfo) {
		Wrapper<CardInfo> queryWrapper = new QueryWrapper<>(userInfo);
		IPage<CardInfo> page = new Page<>(pagination.getPage(), pagination.getPageSize());
		IPage<CardInfo> pageResult = cardInfoService.page(page, queryWrapper);
		LayuiTableResult result = new LayuiTableResult(pageResult.getTotal(), pageResult.getRecords());
		return result;
	}
}
